﻿using System;
using System.Collections;


using mborn.DAL;
using mborn.Model;

namespace mborn.BLL
{
    public class UserBLL
    {
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="user">需添加的User用户实体类</param>
        public void AddUser(User user)
        {
            UserDAL dal = new UserDAL();
            dal.AddUser(user);
        }

        /// <summary>
        /// 更新用户
        /// </summary>
        /// <param name="user">需更新User实体类</param>
        /// <param name="ID">User的ID</param>
        public void UpdateUser(User user, int ID)
        {
            UserDAL dal = new UserDAL();
            dal.UpdateUser(user, ID);
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="user">用户实体</param>
        public void DeleletUser(User user)
        {
            UserDAL dal = new UserDAL();
            dal.DeleteUser(user);
        }

        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <param name="strHQL">hql查询语句</param>
        /// <returns>IList格式的列表</returns>
        public IList GetAllUsers(string strHQL)
        {
            UserDAL dal = new UserDAL();
            return dal.GetAllUsers(strHQL);
        }

        /// <summary>
        /// 判断是否存在用户
        /// </summary>
        /// <param name="Email">用户注册使用Email</param>
        /// <returns>True为用户存在，False为用户不存在</returns>
        public bool IsUserExist(string Email) 
        {
            string strHQL;
            IList IlstUser;

            strHQL = "from mborn.Model.User as tuser where tuser.Email='"+Email+"'";
            IlstUser = GetAllUsers(strHQL);
            
            if(IlstUser.Count > 0) return true ;

            return false ;
            
        }

        /// <summary>
        /// 判断用户登录是否成功
        /// </summary>
        /// <param name="Email">用户登录的email</param>
        /// <param name="Pwd">用户登录的密码</param>
        /// <returns>true为登录成功 false为登录失败</returns>
        public bool  IsUserCanLogin(string Email, string Pwd) 
        {
            string strHQL;
            IList IlstUser;

            strHQL = "from mborn.Model.User as tuser where tuser.Email='" + Email + "' and tuser.Passwd='" + Pwd + "'";
            IlstUser = GetAllUsers(strHQL);

            if (IlstUser.Count <= 0)
                return false;
            else
                return true;

        }

        public string GetUserIDByEmail(string email) 
        {
            string strHQL;
            IList IlstUser;
            User user=new User ();
            int id;

            strHQL = "from mborn.Model.User as user where user.Email='"+email +"'";
            IlstUser = GetAllUsers(strHQL);

            if (IlstUser.Count > 0)
            {
                user = (User)IlstUser[0];
                id = user.ID;
                return Convert.ToString(id);
            }
            else
                return Convert.ToString(0);

        }
    }
}
